#pragma once

#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>

const std::string path = "./dict.txt";  //文件路径
const std::string sep = ":";            //分隔符

static bool Splice(const std::string& s, std::string *s1,  std::string *s2)
{
    auto iter = s.find(sep);
    if(iter == std::string::npos) 
        return false;

    *s1 = s.substr(0, iter);
    *s2 = s.substr(iter + 1);
    return true;
}

class Dict
{
public:
    Dict()
    {
        //打开文件流
        std::ifstream in(path);

        //判断
        if(!in.is_open())
        {
            std::cout << "open dict error" << std::endl;
            exit(1);
        }

        //获取行数据，通过分隔符进行kv配对
        std::string line;
        while(std::getline(in, line))
        {
            std::string str1, str2;//存放临时字符串
            Splice(line, &str1, &str2);//切割字符串
            dict_.insert({str1, str2});
        }
        
    }
    std::string Translation(const std::string& key)
    {   
        auto iter = dict_.find(key);
        if(iter == dict_.end())
            return "Unkonw";
        
        return iter->second;
        
    }

private:
    std::unordered_map<std::string, std::string> dict_;
};